package com.mh.common.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

//自定义填充处理器(MyBatisPlus提供的@TableField配合使用)
@Component //放入Spring容器中
public class MyMetaObjHandler implements MetaObjectHandler {//重写2个方法

    /**BaseEntity类中
     * @ TableField(fill = FieldFill.INSERT_UPDATE) --> updateBy + updateTime属性
     * @ TableField(fill = FieldFill.INSERT) --> createBy + createTime属性
     */

    //insert操作时,需要自动填充的字段
    @Override
    public void insertFill(MetaObject metaObject) {
        //createBy: 创建人，默认值为 "admin"
        this.strictInsertFill(metaObject, "createBy", String.class, "admin");
        this.strictInsertFill(metaObject, "updateBy", String.class, "admin");
        //createTime: 创建时间，默认值为当前时间（new Date()
        this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
        this.strictInsertFill(metaObject, "updateTime", Date.class, new Date());
    }

    //update操作时,需要自动填充的字段
    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictUpdateFill(metaObject, "updateBy", String.class, "admin");
        this.strictUpdateFill(metaObject, "updateTime", Date.class, new Date());
    }
}
